import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;

public class FlightGame extends JFrame implements ActionListener, KeyListener {
    // 游戏核心参数
    private final int WIDTH = 400;
    private final int HEIGHT = 600;
    private int planeX = WIDTH / 2 - 25;
    private final int planeY = HEIGHT - 100;
    private final int planeSize = 50;
    private ArrayList<Rectangle> obstacles = new ArrayList<>();
    private Timer timer;
    private int score = 0;
    private boolean leftPress = false;
    private boolean rightPress = false;
    private Random random = new Random();

    public FlightGame() {
        // 窗口初始化
        setTitle("简易飞行小游戏");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        addKeyListener(this);

        // 定时器（控制游戏帧率）
        timer = new Timer(20, this);
        timer.start();
    }

    // 绘制游戏元素
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        // 背景
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        // 玩家飞机（红色矩形模拟）
        g.setColor(Color.RED);
        g.fillRect(planeX, planeY, planeSize, planeSize);

        // 障碍物（蓝色矩形模拟）
        g.setColor(Color.BLUE);
        for (Rectangle obs : obstacles) {
            g.fillRect(obs.x, obs.y, obs.width, obs.height);
        }

        // 计分板
        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.BOLD, 20));
        g.drawString("Score: " + score, 10, 30);
    }

    // 游戏逻辑更新
    @Override
    public void actionPerformed(ActionEvent e) {
        // 控制飞机移动
        if (leftPress && planeX > 0) planeX -= 5;
        if (rightPress && planeX < WIDTH - planeSize) planeX += 5;

        // 生成障碍物
        if (random.nextInt(30) == 0) {
            int obsWidth = random.nextInt(50) + 30;
            int obsX = random.nextInt(WIDTH - obsWidth);
            obstacles.add(new Rectangle(obsX, 0, obsWidth, 30));
        }

        // 移动障碍物 + 计分
        for (int i = 0; i < obstacles.size(); i++) {
            Rectangle obs = obstacles.get(i);
            obs.y += 4;
            // 障碍物出界移除 + 加分
            if (obs.y > HEIGHT) {
                obstacles.remove(i);
                score++;
                i--;
            }
            // 碰撞检测（游戏结束）
            if (new Rectangle(planeX, planeY, planeSize, planeSize).intersects(obs)) {
                timer.stop();
                JOptionPane.showMessageDialog(this, "游戏结束！最终得分：" + score);
                System.exit(0);
            }
        }

        repaint(); // 重绘界面
    }

    // 键盘事件监听
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) leftPress = true;
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) rightPress = true;
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) leftPress = false;
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) rightPress = false;
    }

    @Override
    public void keyTyped(KeyEvent e) {}

    // 主方法（启动游戏）
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new FlightGame().setVisible(true));
    }
}